﻿using System.Collections.Generic;
using System.Linq;
using Framework.Core.Dependency.Domain;
using Xunit;

namespace Framework.Tests.Core.Dependency
{
    public class DomainTest
    {
        [Fact]
        public void Should_take_difference()
        {
            var modulesFound = new List<Module> { new Module("1", "pipo") };
            var expectedDiff = new Module("2", "pipo");
            var modulesNotFound = new List<Module> { new Module("1", "pipo"), expectedDiff };

            var difference = modulesNotFound.Except(modulesFound).ToList();
            Assert.Contains(expectedDiff, difference);
            Assert.Equal(1, difference.Count());
        }
        
        private IEnumerable<Solution> Dependencies(IEnumerable<Solution> solutions, Solution solution)
        {
            var referencedProjects = solution.Projects
                                             .SelectMany(p => p.Dependencies)
                                             .Select(p => p.Name)
                                             .Distinct()
                                             .ToList();

            if (referencedProjects.Count == 0)
            {
                return Enumerable.Empty<Solution>();
            }

            return solutions
                .Where(s=> !s.Equals(solution))
                .Where(s => SolutionContainsProjects(s, referencedProjects));
        }

        private bool SolutionContainsProjects(Solution solution, List<string> referencedProjects)
        {
            var enumerable = solution.Projects.Select(p => p.Name.ToLower()).ToList();
            var check = referencedProjects.Select(r => r.ToLower());

            return enumerable.Intersect(check).Any();
        }
    }
}